package com.csr.csrcheck.service.impl;

import com.csr.csrcheck.mapper.UserMapper;
import com.csr.csrcheck.pojo.User;
import com.csr.csrcheck.service.IUserService;
import com.csr.csrcheck.service.ex.CompanyException;
import com.csr.csrcheck.service.ex.PasswordNotMatchException;
import com.csr.csrcheck.service.ex.UpdateException;
import com.csr.csrcheck.service.ex.UserNotFoundException;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Random;

@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private UserMapper userMapper;

    public User login(String user_name, String password) {
        User result = userMapper.findUserName(user_name);
        if (result == null)
            throw new UserNotFoundException("用户名或密码错误");
        String md5Password = getMd5Password(password);
        System.err.println("UserServiceImpl.login() ---> ");
        System.err.println("password=" + password);
        System.err.println("md5Password=" + md5Password);
        System.err.println("result.getPassword()=" + result.getPassword());
        if (!result.getPassword().equals(md5Password))
            throw new PasswordNotMatchException("");
        return result;
    }

    public void UpdatePassword(int user_id, String oldpassword, String newpassworld) {
        User user = userMapper.findUser(user_id);
        if (user == null)
            throw new CompanyException("");
        String oldMd5password = getMd5Password(oldpassword);
        if (!user.getPassword().equals(oldMd5password))
            throw new PasswordNotMatchException("");
        String newMd5passworld = getMd5Password(newpassworld);
        Integer row = userMapper.UpdatePassword(user_id, newMd5passworld);
        if (row.intValue() != 1)
            throw new CompanyException("");
    }

    public User finduser(int user_id) {
        User user = userMapper.findUser(user_id);
        if (user == null)
            throw new CompanyException("");
        return user;
    }

    public void UpdateUser(int user_id, String user_name, int user_sex, int user_age, String user_address) {
        Integer row = userMapper.UpdateUser(user_id, user_name, user_sex, user_age, user_address);
        if (row.intValue() != 1)
            throw new UpdateException("");
    }

    public void UpdateUser_photo(int user_id, HttpServletRequest request, HttpSession session, MultipartFile file) {
        String url = null;
        if (!file.isEmpty()) {
            String fileName = file.getOriginalFilename();
            String suffixName = FilenameUtils.getExtension(fileName);
            String filePath = "D://temp-rainy//";
            if (suffixName.equalsIgnoreCase("jpg") || suffixName.equalsIgnoreCase("png") || suffixName
                    .equalsIgnoreCase("jpeg") || suffixName.equalsIgnoreCase("pneg")) {
                fileName = System.currentTimeMillis() + "_" + (new Random()).nextInt(1000) + ".png";
                File dest = new File(filePath + fileName);
                if (!dest.exists())
                    dest.mkdirs();
                try {
                    file.transferTo(dest);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                url = "/temp-rainy/" + fileName;
            } else {
                throw new CompanyException("");
            }
            int row = userMapper.UpdatePhoto(url, user_id).intValue();
            if (row != 1)
                throw new CompanyException("");
        }
    }

    public void addUser(String user_name, String password, int user_sex, int user_age, String user_address, String create_people, Date create_time) {
        String md5password = getMd5Password(password);
        System.out.println(md5password);
//        try {
//            int row = userMapper.addUser(user_name, md5password, user_sex, user_age, user_address, create_people, create_time).intValue();
//            if (row != 1)
//                throw new CompanyException("添加失败");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

    }

    public String getMd5Password(String password) {
        String str = password;
        for (int i = 0; i < 5; i++)
            str = DigestUtils.md5DigestAsHex(str.getBytes()).toUpperCase();
        return str;
    }

    public static void main(String[] args) {
        UserServiceImpl userService = new UserServiceImpl();
        System.out.println(userService.getMd5Password("admin"));
    }
}
